package com.swt.shoppingmall.shoppingcart.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.swt.shoppingmall.R;
import com.swt.shoppingmall.home.bean.GoodsBean;
import com.swt.shoppingmall.shoppingcart.utils.CartStorage;
import com.swt.shoppingmall.utils.CommonUtils;
import com.swt.shoppingmall.utils.Constants;
import com.swt.shoppingmall.view.NumberAddSubView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by swt on 2016/12/27.
 */
public class ShoppingCartAdapter extends RecyclerView.Adapter<ShoppingCartAdapter.ViewHolder> {

    private Activity mActivity;
    private List<GoodsBean> goodsBeanList;
    private TextView tvShopcartTotal;
    private CheckBox checkboxAll;
    private CheckBox cbAll;//完成状态下的删除CheckBox

    public ShoppingCartAdapter(Activity activity, List<GoodsBean> goodsBeanList, TextView tvShopcartTotal, CheckBox checkboxAll, CheckBox cbAll) {
        this.mActivity = activity;
        this.goodsBeanList = goodsBeanList;
        this.tvShopcartTotal = tvShopcartTotal;
        this.checkboxAll = checkboxAll;
        this.cbAll = cbAll;
        showTotalPrice();
        //设置点击事件
        setListener();
        //校验是否全选中
        checkAll();
    }

    private void setListener() {
        setOnItenClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //根据位置找到对应的Bean对象
                GoodsBean goodsBean = goodsBeanList.get(position);
                //设置CheckBox取反状态
                goodsBean.setSeleted(!goodsBean.isSeleted());
                //刷新页面
                notifyItemChanged(position);
                //校验是否全选
                checkAll();
                //重新计算总价
                showTotalPrice();
            }
        });


        //设置checkbox的点击事件
        checkboxAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //得到状态
                boolean isCheck = checkboxAll.isChecked();

                //根据状态设置全选/非全选按钮
                checkAll_None(isCheck);
                //计算价格
                showTotalPrice();
            }
        });

        //设置checkbox的点击事件
        cbAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //得到状态
                boolean isCheck = cbAll.isChecked();

                //根据状态设置全选/非全选按钮
                checkAll_None(isCheck);
            }
        });
    }

    //设置全选和非全选
    public void checkAll_None(boolean isCheck) {
        if (goodsBeanList != null && goodsBeanList.size() > 0) {
            for (int i = 0; i < goodsBeanList.size(); i++) {
                //全选
                goodsBeanList.get(i).setSeleted(isCheck);
                notifyItemChanged(i);
            }
        }
    }


    public void checkAll() {
        if (goodsBeanList != null && goodsBeanList.size() > 0) {
            int number = 0;
            for (int i = 0; i < goodsBeanList.size(); i++) {
                if (!goodsBeanList.get(i).isSeleted()) {
                    //非全选
                    checkboxAll.setChecked(false);
                    cbAll.setChecked(false);
                } else {
                    //选中的
                    number++;
                }
            }

            if (number == goodsBeanList.size()) {
                //全选
                checkboxAll.setChecked(true);
                cbAll.setChecked(true);
            }
        } else {
            checkboxAll.setChecked(false);
            cbAll.setChecked(false);
        }
    }

    /**
     * 设置总价
     */
    public void showTotalPrice() {
        tvShopcartTotal.setText(getTotalPrice());
    }

    /**
     * 计算总价
     *
     * @return
     */
    public String getTotalPrice() {
        double totalPrice = 0.00;
        if (goodsBeanList != null && goodsBeanList.size() > 0) {
            for (int i = 0; i < goodsBeanList.size(); i++) {
                GoodsBean goodsBean = goodsBeanList.get(i);
                if (goodsBean.isSeleted()) {//被选中的
                    totalPrice = totalPrice + (Double.parseDouble(goodsBean.getCover_price()) * goodsBean.getNumber());
                }
            }
        }
        return CommonUtils.getInstance().doubleFormat(totalPrice);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mActivity, R.layout.item_shop_cart, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final GoodsBean goodsBean = goodsBeanList.get(position);
        holder.cb_gov.setChecked(goodsBean.isSeleted());
        Glide.with(mActivity).load(Constants.BASE_URL_IMAGE + goodsBean.getFigure()).into(holder.iv_gov);
        holder.tv_desc_gov.setText(goodsBean.getName());
        holder.tv_price_gov.setText(mActivity.getString(R.string.rmb) + goodsBean.getCover_price());

        holder.numberAddSubView.setValue(goodsBean.getNumber());//设置数量
        holder.numberAddSubView.setMinValue(1);//设置最小数量
        holder.numberAddSubView.setMaxValue(10);//设置库存最大数量

        //设置商品数量的变化
        holder.numberAddSubView.setOnNumberChangerListener(new NumberAddSubView.OnNumberChangerListener() {
            @Override
            public void onNumberChange(int value) {
                //更新内存和本地sp中的数据
                goodsBean.setNumber(value);
                CartStorage.getInstance().updateData(goodsBean);
                //刷新适配器中单条数据
                notifyItemChanged(position);
                //再次计算总价
                showTotalPrice();
            }
        });

    }

    @Override
    public int getItemCount() {
        return goodsBeanList == null ? 0 : goodsBeanList.size();
    }

    //删除选中的数据
    public void deleteData() {
        if (goodsBeanList != null && goodsBeanList.size() > 0) {
            for (int i = 0; i < goodsBeanList.size(); i++) {
                GoodsBean goodsBean = goodsBeanList.get(i);
                if (goodsBean.isSeleted()) {//删除被选中的
                    //内存中移除
                    goodsBeanList.remove(i);
                    //保存本地
                    CartStorage.getInstance().deleteData(goodsBean);
                    //刷新
                    notifyItemRemoved(i);
                    i--;
                }
            }
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private final CheckBox cb_gov;
        private final ImageView iv_gov;
        private final TextView tv_desc_gov;
        private final TextView tv_price_gov;
        private final NumberAddSubView numberAddSubView;

        public ViewHolder(View itemView) {
            super(itemView);
            cb_gov = (CheckBox) itemView.findViewById(R.id.cb_gov);
            iv_gov = (ImageView) itemView.findViewById(R.id.iv_gov);
            tv_desc_gov = (TextView) itemView.findViewById(R.id.tv_desc_gov);
            tv_price_gov = (TextView) itemView.findViewById(R.id.tv_price_gov);
            numberAddSubView = (NumberAddSubView) itemView.findViewById(R.id.numberAddSubView);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(getLayoutPosition());
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItenClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
